package com.maxuxu.scihome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxuxu.scihome.mapper.UserMapper;
import com.maxuxu.scihome.pojo.Pages;
import com.maxuxu.scihome.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: UserService
 * @author: Maxuxu
 * @date: 2022/6/29 16:27
 * @description:
 */
@Service
@Slf4j

public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    Pages pages;
    /**
     * 查找指定用户数据，已弃用，下面的方法可以通过任意条件查询，复用性更高
     * @param loginname
     * @return
     */
    // public User selectUser(String loginname){
    //     QueryWrapper<User> wrapper = new QueryWrapper<>();
    //     wrapper.eq("loginname",loginname);
    //     User user = userMapper.selectOne(wrapper);
    //     return user;
    // }

    /**
     * 通过任意条件查询用户信息
     * @param user2
     * @return
     */
    public User selectUserByAny(User user2){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        log.error("用户登录信息{}",user2.getLoginname());
        if (user2.getLoginname()!=null){
            wrapper.eq("loginname", user2.getLoginname());
        }
        if (user2.getStatus()!=null){
            wrapper.eq("status", user2.getStatus());
        }
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    /**
     * 更新用户数据
     * @param loginname
     * @param user
     */
    public void updateUser(String loginname,User user){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("loginname", loginname);
        userMapper.update(user,wrapper);
    }

    /**
     * 插入用户
     * @param user
     * @return
     */
    public String insertUser(User user){
        userMapper.insert(user);
        return "插入成功";
    }

    /**
     * 查询所有用户
     * @return
     */
    public List<User> queryAll(User user){
        Integer status = user.getStatus();

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (status!=null){
            wrapper.eq("status", status);
        }
        List<User> users = userMapper.selectList(wrapper);

        return users;
    }

    /**
     * 通过登录名删除用户
     * @param name
     * @return
     */
    public String deleteUserByName(String name){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("loginname", name);
        userMapper.delete(wrapper);
        return "删除成功";
    }

    /**
     * 通过用户id批量删除用户，当然也可以删除一个
     * @param list
     * @return
     */
    public String deleteUserByids(List<Integer> list){
        userMapper.deleteBatchIds(list);
        return "删除成功";
    }

    /**
     * 分页查询，分别获得列表数据，总数，总页数，当前页，每页限制
     * @param current
     * @param size
     * @return
     */

    public Pages selectPage(long current, long size){
        Page<User> userPage = userMapper.selectPage(new Page<>(current, size), null);
        List<User> records = userPage.getRecords();
        long total = userPage.getTotal();
        long pages = userPage.getPages();
        long current1 = userPage.getCurrent();
        long size1 = userPage.getSize();
        this.pages.setPages(pages);
        this.pages.setCurrent(current1);
        this.pages.setSize(size1);
        this.pages.setTotal(total);
        this.pages.setRecords(records);
        return this.pages;
    }
}
